package com.ym.jiujiangeyou.adapter;

import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

import com.ym.jiujiangeyou.R;
import com.ym.jiujiangeyou.bitmap.ImageLoader;
import com.ym.jiujiangeyou.ui.ImageViewEx;
import com.ym.jiujiangeyou.util.Util;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Gallery;

/**
 * @author 叶铭
 * @email yeming_1001@163.com
 * @Description: 图片画廊适配器
 */
public class GalleryAdapter extends BaseAdapter {

	private Context context;
	private List<String> items = new ArrayList<String>();
	private ImageLoader imageLoader;

	public void setData(List<String> data) {
		this.items = data;
		notifyDataSetChanged();
	}

	public GalleryAdapter(Context context) {
		this.context = context;

		imageLoader = new ImageLoader(context);
		int size = Math.min(Util.getScreenWidth(context),
				Util.getScreenHeight(context));
		imageLoader.setScaleParam(size);
		imageLoader.setDefaultBitmap(context.getResources().getDrawable(
				R.drawable.load_img));
		imageLoader.setLoadLocalBitmapQuick(true);
	}

	@Override
	public int getCount() {
		return items != null ? items.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressWarnings("deprecation")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ImageViewEx view = null;
		if (convertView == null) {
			view = new ImageViewEx(context);
		} else {
			view = (ImageViewEx) convertView;
		}
		view.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		String url = items.get(position);
		view.setDefaultBitmapFlag(true);
		boolean ret = imageLoader.DisplayImage(url, view, true);
		view.setDefaultBitmapFlag(!ret);
		view.setURL(url);
		return view;
	}

	public boolean syncRefreshImageViewEx(ImageViewEx imageViewEx) {
		return imageLoader.syncLoadBitmap(imageViewEx.getURL(), imageViewEx);
	}

}
